/*
 * 组织管理
 */
Ext.onReady(function(){
	
	Ext.QuickTips.init();
	//树节点ID 树节点名称
	var typeWin_docId="0000", typeWin_docSubject="", v_start=0, v_limit=20;
	
	var mapObj = [
		{ name:'docId', type:'string'},
		{ name:'docSubject', type:'string'},
		{ name:'code', type:'string'},
		{ name:'parentName', type:'string'},
		{ name:'parentId', type:'string'},
		{ name:'level', type:'string'},
		{ name:'pnum', type:'int'},
		{ name:'groupName', type:'string'}
	];
	
	//组织数据
	var store = new Ext.data.JsonStore({
	    url: i1510.contextPath+'/admin/sysOrg/listExt.do',
	    root: 'root',
	    totalProperty: 'total',
	    fields: mapObj,
	    listeners:{beforeload:function(a){a.baseParams={start:v_start, limit:v_limit,parentId:typeWin_docId,docStatus:"10"};}}
	});
	
	//组织列表
    var grid = new Ext.grid.GridPanel({
        store: store,
        cm: new Ext.grid.ColumnModel({
			defaults: {	menuDisabled : true},//禁止表头菜单
			columns:[new Ext.grid.RowNumberer(),
				{header: '组织ID', width: 100, sortable:true, dataIndex: 'docId',hidden:true},
	            {header: '组织名称', width: 130, sortable:true, dataIndex: 'docSubject'},
	            {header: '组织编码', width: 80, sortable:true, dataIndex: 'code'},
	            {header: '上级名称', width: 60, sortable:true, dataIndex: 'parentName'},
	            {header: '上级Id', width: 60, sortable:true, dataIndex: 'parentId',hidden:true},
	            {header: '组织层次', width: 60, sortable:true, dataIndex: 'level'},
	            {header: '组织人数', width: 60, sortable:true, dataIndex: 'pnum'},
	            {header: '所属集团', width: 60, sortable:true, dataIndex: 'groupName'}
	        ]
        }),
        stripeRows: true, 	//行分隔符
        columnLines : true, //列分隔符
		frame:true,
		region:'center',
		title:'组织信息',
        iconCls:'menu-53',
        
        tbar:[{
        	text:'增加',
        	iconCls:'btn-add',
        	handler: function(){
        		fn_add_typeWin();
        	}
        },'-',{
        	text:'修改',
        	iconCls:'btn-edit',
        	handler: function(){
        		fn_update_typeWinList();
        	}
        },'-',{
        	text:'删除',
        	iconCls:'btn-remove',
        	handler: function(){
        		fn_del_typeWinList();
        	}
        }],
        
        bbar: new Ext.PagingToolbar({
            pageSize: v_limit,
            store: store,
            displayInfo: true
        }),
        
        listeners:{
        	rowdblclick:function(){
        		fn_update_typeWinList();
        	}
        }
    });
	//新增typeWin
	function fn_add_typeWin(){
		typeWin.show();
		typeForm.getForm().reset();
	}
	function fn_update_typeWinList(){
		var record= grid.getSelectionModel().getSelected(); 
		if(!record){
        	Ext.Msg.alert('信息提示','请选择要修改的组织');
		}else{
			typeWin.show();
			typeForm.getForm().loadRecord(record);
		}
	}
	function fn_del_typeWinList(){
		var record= grid.getSelectionModel().getSelected();
		if(!record){
        	Ext.Msg.alert('信息提示','请选择要删除的记录');  
		}else{
			Ext.MessageBox.confirm('删除提示', '是否删除该记录？', function(c) {
			   if(c=='yes'){
			   		Ext.Ajax.request({
			   			url : i1510.contextPath+'/admin/sysOrg/updateStatus.do',
			   			params:{  docId : record.get("docId"),docStatus:"00", },
			   			success : function(rtn) {
			   				var temp = Ext.util.JSON.decode(rtn.responseText);
			   				if(temp.code =="0"){
			   					Ext.Msg.alert("信息提示","该类别下有信息不能删除");
			   				}else{
				   				store.reload();
			   				}
			   			}
			   		});
			    }
			});
		}
	}
	//删除typeWin
	function fn_del_typeWin(){
		
		if(typeWin_docId){
    		var node = tree.getNodeById(typeWin_docId);
    		var pnode = node.parentNode;
			Ext.MessageBox.confirm('删除提示', '是否删除"'+node.text+'"类别？', function(c) {
			   if(c=='yes'){
			   		Ext.Ajax.request({
			   			url : i1510.contextPath+'/admin/sysOrg/updateStatus.do',
			   			params:{ docId : typeWin_docId,docStatus:"00" },
			   			success : function(rtn) {
			   				var temp = Ext.util.JSON.decode(rtn.responseText);
			   				if(temp.code =="0"){
			   					Ext.Msg.alert("信息提示","该类别有组织信息不能删除");
			   				}else{
				   				typeWin_docId = "0000";     //设为默认节点
				   				typeWin_docSubject = "";
				   				tree.buttons[1].setDisabled(true);  //禁用删除按钮
				   				pnode.removeChild(node);	//删除节点
				   				if(pnode.childNodes.length==0){	//如果无子节点则修改属性
				   					pnode.leaf = true;
				   				}
				   				
			   				}
			   			}
			   		});
			    }
			});
		}
	}
	//组织类别树
	var tree = new Ext.tree.TreePanel({
		title:'组织类别',
		region : 'west',
		width : 180,
        minSize: 150,
        maxSize: 300,
        split : true,
		useArrows: true,
        autoScroll:true,
        animate:true,
        enableDD:false,
        containerScroll: true,
        frame:true,
        dataUrl: i1510.contextPath+'/admin/sysOrg/findTree.do?docStatus=10',
        root: {
            nodeType: 'async',
            text: '所有组织',
            draggable: false,
            id: '0000'
        },
        buttonAlign : 'left',
        buttons:[{
        	text:'新增组织',
        	handler:function(){
        		fn_add_typeWin();
        	}
        },{
        	text:'删除组织',
        	disabled:true,
        	handler:function(){
        		fn_del_typeWin();
        	}
        }],
        listeners:{
        	click:function(n){
        		//获取点击后的树节点ID 和 text
        		typeWin_docId = n.id;
        		typeWin_docSubject = n.text;
        		//设置删除按钮状态 下级不能有子节点
        		if(!n.leaf||typeWin_docId=="0000"){
        			tree.buttons[1].setDisabled(true);
        		}else{
        			tree.buttons[1].setDisabled(false);
        		}
        		//更新组织数据
        		store.load({params:{start:v_start, limit:v_limit,parentId:typeWin_docId,docStatus:"10"}});
        	}
        }
	});
	
	//展开节点
	tree.getRootNode().expand();
	
	//组织类别表单
	var typeForm = new Ext.FormPanel({
		layout : 'form',
		baseCls: 'x-plain',
		labelWidth:60,
		border : false,
		padding : 20,
		items:[{
			xtype:'textfield',
			anchor : '100%',
			name:'docSubject',
			fieldLabel:'组织名称',
			allowBlank : false,
			maxLength :20
		},{
			xtype:'textfield',
			anchor : '100%',
			name:'code',
			fieldLabel:'组织编码',
			allowBlank : true,
			maxLength :200
		},{
			xtype:'textfield',
			anchor : '100%',
			name:'level',
			fieldLabel:'组织层次',
			allowBlank : true,
			maxLength :200
		},{
			xtype : 'numberfield',
			anchor : '100%',
			name : 'pnum',
			fieldLabel : '组织人数',
			allowBlank : false,
			maxLength : 4
		},{
			xtype : 'hidden',
		    name : 'docId'
		}]
	});
 
	//提交typeWin
	var fn_sumbit_typeWin = function(next){

		if (typeForm.getForm().isValid()) {
			typeForm.getForm().submit({
			    url: i1510.contextPath+'/admin/sysOrg/save.do',
				params:{parentId : typeWin_docId},
				success : function(form, rtn) {
					
					var id = rtn.result.docId;

					var existNode = tree.getNodeById(id);
					if(existNode != null){
						existNode.text = form.findField("docSubject").getValue();
						//更新树跟节点
						tree.root.reload();
					}else{
						//创建新节点
						var node = new Ext.tree.TreeNode({
							id:id,
							text:form.findField("docSubject").getValue(),
							iconCls:'menu-folder',
							leaf:true
						});
						//修改父节点
						var pnode = tree.getNodeById(typeWin_docId);
						pnode.appendChild(node);
						pnode.leaf=false;
						pnode.expand();
					}
					
				   	if(next){
				   		typeWin.getForm().reset();
					}else{
						typeWin.hide();
					}
				   	//刷新list
				   	store.reload();
				},
				failure : function(form, rtn) {
					Ext.Msg.alert('信息提示','连接失败.'+rtn.result.message);
				},
				waitTitle : '提交',
				waitMsg : '正在保存数据，稍后...'
			});
		}
	}
	//增加组织类别窗口
    var typeWin = new Ext.Window({
		title : '新增/修改',
		width:250,
		height:200,
		closeAction:'hide',
		modal : true,
		resizable : false,
		layout : 'fit',
		buttonAlign : 'center',
		items : [typeForm],
		buttons : [{
			text : '新增下一组织',
			handler : function() {
				fn_sumbit_typeWin(true);
			}
		},{
			text : '保存',
			handler : function() {
				fn_sumbit_typeWin(false);
			}
		}, {
			text : '取消',
			handler : function() {
				typeWin.hide();
			}
		}]
	});
    
	//布局
    new Ext.Viewport({
		layout:'fit',
		items:[{
			frame:true,
			layout:'border',
			items:[tree,grid]
		}]
	});

});